/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import Datos.Conexion;
import Datos.Global;
import Negocio.clsCategoria;
import Negocio.clsInsumo;
import java.awt.Image;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author RODRIGO
 */
public class frmInsumo extends javax.swing.JDialog {

    //Conexion conn = new Conexion();
    String urlFoto;
    int codigo;
    JFileChooser jfc = new JFileChooser();
    private boolean bModificar;

    public frmInsumo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        this.setTitle("Insumo");
        clsInsumo.mostrarTablaInsumos(jtbInsumo, null);
        clsInsumo.mostrarCboCategoria(cboMostrar);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btnNuevo = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        lblCategoria = new javax.swing.JLabel();
        lblNombre = new javax.swing.JLabel();
        lblValorProteico = new javax.swing.JLabel();
        lblStock = new javax.swing.JLabel();
        lblUnidad = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtValorProteico = new javax.swing.JTextField();
        cboUnidad = new javax.swing.JComboBox();
        ftfStock = new javax.swing.JFormattedTextField();
        lblSustituto = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSustituto = new jclist.jcList();
        txtCategoria = new javax.swing.JTextField();
        btnAgregarSustituto = new javax.swing.JButton();
        lblImagen = new javax.swing.JLabel();
        btnQuitarSustituto = new javax.swing.JButton();
        btnCategoria = new javax.swing.JButton();
        lblPrioridad = new javax.swing.JLabel();
        cboPrioridad = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbInsumo = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        cboMostrar = new javax.swing.JComboBox();
        txtBuscar = new javax.swing.JTextField();
        lblBuscar = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));

        jPanel2.setBackground(new java.awt.Color(102, 102, 255));

        jToolBar1.setBackground(new java.awt.Color(0, 102, 204));
        jToolBar1.setBorder(null);
        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);
        jToolBar1.setBorderPainted(false);
        jToolBar1.setOpaque(false);

        btnNuevo.setFont(new java.awt.Font("Cambria", 1, 14)); // NOI18N
        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/add.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.setFocusable(false);
        btnNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNuevo.setOpaque(false);
        btnNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNuevo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnNuevoMouseClicked(evt);
            }
        });
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnNuevo);

        btnModificar.setFont(new java.awt.Font("Cambria", 1, 14)); // NOI18N
        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/refresh.png"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.setEnabled(false);
        btnModificar.setFocusable(false);
        btnModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnModificar.setOpaque(false);
        btnModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnModificar);

        btnGuardar.setFont(new java.awt.Font("Cambria", 1, 14)); // NOI18N
        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/save.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.setEnabled(false);
        btnGuardar.setFocusable(false);
        btnGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnGuardar.setOpaque(false);
        btnGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnGuardar);

        btnCancelar.setFont(new java.awt.Font("Cambria", 1, 14)); // NOI18N
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/remove.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnCancelar.setFocusable(false);
        btnCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancelar.setOpaque(false);
        btnCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnCancelar);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 89, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE))
        );

        jPanel3.setBackground(new java.awt.Color(153, 153, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos relacionados con el Insumo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Cambria", 1, 14))); // NOI18N

        lblCategoria.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblCategoria.setText("Categoria:");
        lblCategoria.setPreferredSize(new java.awt.Dimension(80, 20));

        lblNombre.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblNombre.setText("Nombre:");
        lblNombre.setPreferredSize(new java.awt.Dimension(80, 20));

        lblValorProteico.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblValorProteico.setText("Valor Proteico:");
        lblValorProteico.setPreferredSize(new java.awt.Dimension(80, 20));

        lblStock.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblStock.setText("Stock Actual:");
        lblStock.setPreferredSize(new java.awt.Dimension(80, 20));

        lblUnidad.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblUnidad.setText("Unidad:");
        lblUnidad.setPreferredSize(new java.awt.Dimension(80, 20));

        txtNombre.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtNombre.setEnabled(false);
        txtNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNombreKeyReleased(evt);
            }
        });

        txtValorProteico.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtValorProteico.setEnabled(false);
        txtValorProteico.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtValorProteicoKeyReleased(evt);
            }
        });

        cboUnidad.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        cboUnidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kilogramos (Kg)", "Gramos (gr)", "Litros (Lt)", "Mililitros (ml)", "Unidades (u)" }));
        cboUnidad.setEnabled(false);

        ftfStock.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        ftfStock.setEnabled(false);
        ftfStock.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        ftfStock.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ftfStockKeyTyped(evt);
            }
        });

        lblSustituto.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblSustituto.setText("Sustitutos:");
        lblSustituto.setPreferredSize(new java.awt.Dimension(80, 20));

        lstSustituto.setBackground(new java.awt.Color(240, 240, 240));
        lstSustituto.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstSustituto.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jScrollPane2.setViewportView(lstSustituto);

        txtCategoria.setEditable(false);
        txtCategoria.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N

        btnAgregarSustituto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/forward2.png"))); // NOI18N
        btnAgregarSustituto.setEnabled(false);
        btnAgregarSustituto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarSustitutoActionPerformed(evt);
            }
        });

        lblImagen.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblImagen.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblImagen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/picture.png"))); // NOI18N
        lblImagen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblImagen.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        lblImagen.setDebugGraphicsOptions(javax.swing.DebugGraphics.BUFFERED_OPTION);
        lblImagen.setDoubleBuffered(true);
        lblImagen.setEnabled(false);
        lblImagen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblImagen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblImagenMouseClicked(evt);
            }
        });

        btnQuitarSustituto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/next2.png"))); // NOI18N
        btnQuitarSustituto.setEnabled(false);
        btnQuitarSustituto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarSustitutoActionPerformed(evt);
            }
        });

        btnCategoria.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/agregarT.png"))); // NOI18N
        btnCategoria.setEnabled(false);
        btnCategoria.setOpaque(false);
        btnCategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCategoriaActionPerformed(evt);
            }
        });

        lblPrioridad.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblPrioridad.setText("Prioridad:");
        lblPrioridad.setPreferredSize(new java.awt.Dimension(80, 20));

        cboPrioridad.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        cboPrioridad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Baja", "Media", "Alta" }));
        cboPrioridad.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(lblCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(btnCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblValorProteico, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblStock, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtValorProteico, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ftfStock, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(lblImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(lblPrioridad, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cboPrioridad, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(lblSustituto, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAgregarSustituto)
                            .addComponent(btnQuitarSustituto))))
                .addContainerGap(11, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(lblCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCategoria))
                .addGap(10, 10, 10)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtValorProteico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblValorProteico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ftfStock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblStock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboPrioridad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPrioridad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblSustituto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(btnAgregarSustituto, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(btnQuitarSustituto, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(16, 16, 16))
        );

        jScrollPane1.setBackground(new java.awt.Color(0, 102, 204));

        jtbInsumo.setBackground(new java.awt.Color(204, 204, 255));
        jtbInsumo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jtbInsumo.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jtbInsumo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idInsumo", "Insumo", "Valor Proteico", "Stock", "Unidad", "Prioridad", "Foto", "Categoria"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtbInsumo.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jtbInsumo.setGridColor(new java.awt.Color(0, 0, 0));
        jtbInsumo.setOpaque(false);
        jtbInsumo.setRowHeight(20);
        jtbInsumo.setSelectionBackground(new java.awt.Color(0, 0, 255));
        jtbInsumo.setSelectionForeground(new java.awt.Color(204, 204, 204));
        jtbInsumo.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jtbInsumo.getTableHeader().setReorderingAllowed(false);
        jtbInsumo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbInsumoMouseClicked(evt);
            }
        });
        jtbInsumo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jtbInsumoFocusLost(evt);
            }
        });
        jScrollPane1.setViewportView(jtbInsumo);
        jtbInsumo.getColumnModel().getColumn(0).setMinWidth(0);
        jtbInsumo.getColumnModel().getColumn(0).setPreferredWidth(0);
        jtbInsumo.getColumnModel().getColumn(0).setMaxWidth(0);
        jtbInsumo.getColumnModel().getColumn(1).setPreferredWidth(150);
        jtbInsumo.getColumnModel().getColumn(2).setMinWidth(0);
        jtbInsumo.getColumnModel().getColumn(2).setPreferredWidth(0);
        jtbInsumo.getColumnModel().getColumn(2).setMaxWidth(0);
        jtbInsumo.getColumnModel().getColumn(3).setPreferredWidth(40);
        jtbInsumo.getColumnModel().getColumn(4).setMinWidth(0);
        jtbInsumo.getColumnModel().getColumn(4).setPreferredWidth(0);
        jtbInsumo.getColumnModel().getColumn(4).setMaxWidth(0);
        jtbInsumo.getColumnModel().getColumn(5).setMinWidth(0);
        jtbInsumo.getColumnModel().getColumn(5).setPreferredWidth(0);
        jtbInsumo.getColumnModel().getColumn(5).setMaxWidth(0);
        jtbInsumo.getColumnModel().getColumn(6).setMinWidth(0);
        jtbInsumo.getColumnModel().getColumn(6).setPreferredWidth(0);
        jtbInsumo.getColumnModel().getColumn(6).setMaxWidth(0);
        jtbInsumo.getColumnModel().getColumn(7).setPreferredWidth(50);

        jPanel5.setBackground(new java.awt.Color(153, 153, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));

        cboMostrar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        cboMostrar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TODOS" }));
        cboMostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboMostrarActionPerformed(evt);
            }
        });

        txtBuscar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarKeyReleased(evt);
            }
        });

        lblBuscar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/lupa.png"))); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cboMostrar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblBuscar)
                .addContainerGap(16, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cboMostrar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnNuevoMouseClicked
    }//GEN-LAST:event_btnNuevoMouseClicked

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        limpiarCampos();
        habilitarCampos(true);
        btnGuardar.setEnabled(true);
        btnModificar.setEnabled(false);
        bModificar = false;
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        btnModificar.setEnabled(false);
        limpiarCampos();
        habilitarCampos(true);
        btnGuardar.setEnabled(true);
        bModificar = true;

        int row = jtbInsumo.getSelectedRow();
        codigo = Integer.valueOf(jtbInsumo.getValueAt(row, 0).toString());
        txtNombre.setText(jtbInsumo.getValueAt(row, 1).toString());
        txtValorProteico.setText(jtbInsumo.getValueAt(row, 2).toString());
        ftfStock.setText(jtbInsumo.getValueAt(row, 3).toString());
        cboUnidad.setSelectedItem(jtbInsumo.getValueAt(row, 4));
        cboPrioridad.setSelectedItem(jtbInsumo.getValueAt(row, 5));
        Object url = jtbInsumo.getValueAt(row, 6);
        txtCategoria.setText(jtbInsumo.getValueAt(row, 7).toString());

        if (url != null) {
            urlFoto = url.toString();
            ImageIcon ic = new ImageIcon(urlFoto);
            Icon icono = new ImageIcon(ic.getImage().getScaledInstance(lblImagen.getWidth(), lblImagen.getHeight(), Image.SCALE_DEFAULT));
            lblImagen.setIcon(icono);
        }

        DefaultListModel dlm = new DefaultListModel();
        clsInsumo.mostrarSustitutos(dlm, codigo + "");
        lstSustituto.setModel(dlm);

    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed

        btnModificar.setEnabled(false);
        btnGuardar.setEnabled(false);
        txtBuscar.setText(null);

        String nombre = txtNombre.getText();
        float valor_proteico = Float.valueOf(txtValorProteico.getText().replace(",", "."));
        float stock = Float.valueOf(ftfStock.getText().toString().replace(",", "."));
        String unidad = (String) cboUnidad.getSelectedItem();
        String prioridad = cboPrioridad.getSelectedItem().toString();
        int idCategoria = clsCategoria.getIdCategoria(txtCategoria.getText());//Integer.valueOf(jtbInsumo.getValueAt(jtbInsumo.getSelectedRow(), 0).toString());

        if (bModificar) {
            clsInsumo.actualizarInsumo(codigo, nombre, valor_proteico, stock, unidad, prioridad, urlFoto, idCategoria);
        } else {
            clsInsumo.insertarInsumo(nombre, valor_proteico, stock, unidad, prioridad, urlFoto, txtCategoria.getText());
        }

        clsInsumo.eliminarSustituto(codigo);
        for (int i = 0; i < lstSustituto.getModel().getSize(); i++) {
            int sust = clsInsumo.getIdInsumo(lstSustituto.getModel().getElementAt(i).toString());
            if (sust > 0) {
                clsInsumo.insertarSustitutos(codigo, sust);
            }
        }

        bModificar = false;
        habilitarCampos(false);
        limpiarCampos();
        //        cboMostrarActionPerformed(evt);
        //        String sql = "SELECT `idInsumo`,`nombre`,`stock` FROM `insumo` ORDER BY `nombre`";
        //        insumo.mostrarTablaInsumos(jtbInsumo, sql);
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        limpiarCampos();
        habilitarCampos(false);
        btnGuardar.setEnabled(false);
        btnModificar.setEnabled(false);

    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtNombreKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreKeyReleased
        Global.validarLetra(50, evt, txtNombre);
    }//GEN-LAST:event_txtNombreKeyReleased

    private void txtValorProteicoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtValorProteicoKeyReleased
        Global.validarNumero((int) Float.MAX_VALUE, evt, txtValorProteico);
    }//GEN-LAST:event_txtValorProteicoKeyReleased

    private void ftfStockKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ftfStockKeyTyped
        Global.validarNumero((int) Float.MAX_VALUE, evt, ftfStock);
    }//GEN-LAST:event_ftfStockKeyTyped

    private void btnAgregarSustitutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarSustitutoActionPerformed
        int row = jtbInsumo.getSelectedRow();
        if (row >= 0) {
            DefaultListModel dlm = (DefaultListModel) lstSustituto.getModel();
            Object o = jtbInsumo.getValueAt(row, 1);
            if (!dlm.contains(o)) {
                dlm.addElement(o);
            }
            lstSustituto.setModel(dlm);
        }
    }//GEN-LAST:event_btnAgregarSustitutoActionPerformed

    private void lblImagenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblImagenMouseClicked
        if (!lblImagen.isEnabled()) {
            return;
        }
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.setFileFilter(new FileNameExtensionFilter("Imagenes", "png", "jpg", "jpge", "bmp"));
        if (jfc.showDialog(this, "Abrir") == JFileChooser.APPROVE_OPTION) {
            //            lblImagen.setIcon(new ImageIcon(jfc.getSelectedFile().getAbsolutePath()));
            urlFoto = jfc.getSelectedFile().getAbsolutePath();
            ImageIcon ic = new ImageIcon(urlFoto);
            int w = lblImagen.getWidth();
            int h = lblImagen.getHeight();
            Icon icono = new ImageIcon(ic.getImage().getScaledInstance(w, h, Image.SCALE_DEFAULT));
            lblImagen.setIcon(icono);
        }
    }//GEN-LAST:event_lblImagenMouseClicked

    private void btnQuitarSustitutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarSustitutoActionPerformed
        int row = lstSustituto.getSelectedIndex();
        if (row >= 0) {
            DefaultListModel dlm = (DefaultListModel) lstSustituto.getModel();
            dlm.remove(row);
            lstSustituto.setModel(dlm);
        }
    }//GEN-LAST:event_btnQuitarSustitutoActionPerformed

    private void btnCategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCategoriaActionPerformed
        //Llama a frmBuscarCategoria para seleccionar una categoria
        new frmBuscarCategoria(null, true).setVisible(true);
        txtCategoria.setText(frmBuscarCategoria.txtBuscar.getText());
    }//GEN-LAST:event_btnCategoriaActionPerformed

    private void cboMostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboMostrarActionPerformed
        String sql = "";
        if (cboMostrar.getSelectedIndex() >= 0) //{
        {
            sql = cboMostrar.getSelectedItem().toString();
        }
        if (sql.equalsIgnoreCase("todos")) {
            sql = "";
        }
        clsInsumo.mostrarTablaInsumosCategoria(jtbInsumo, sql);
    }//GEN-LAST:event_cboMostrarActionPerformed

    private void txtBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyReleased
        String like = txtBuscar.getText().trim();
        clsInsumo.mostrarTablaInsumos(jtbInsumo, like);

    }//GEN-LAST:event_txtBuscarKeyReleased

    private void jtbInsumoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbInsumoMouseClicked
        btnModificar.setEnabled(true);
    }//GEN-LAST:event_jtbInsumoMouseClicked

    private void jtbInsumoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtbInsumoFocusLost
        btnModificar.setEnabled(false);
    }//GEN-LAST:event_jtbInsumoFocusLost

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmInsumo dialog = new frmInsumo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarSustituto;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnCategoria;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnQuitarSustituto;
    private javax.swing.JComboBox cboMostrar;
    private javax.swing.JComboBox cboPrioridad;
    private javax.swing.JComboBox cboUnidad;
    private javax.swing.JFormattedTextField ftfStock;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable jtbInsumo;
    private javax.swing.JLabel lblBuscar;
    private javax.swing.JLabel lblCategoria;
    private javax.swing.JLabel lblImagen;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblPrioridad;
    private javax.swing.JLabel lblStock;
    private javax.swing.JLabel lblSustituto;
    private javax.swing.JLabel lblUnidad;
    private javax.swing.JLabel lblValorProteico;
    private jclist.jcList lstSustituto;
    private javax.swing.JTextField txtBuscar;
    private javax.swing.JTextField txtCategoria;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtValorProteico;
    // End of variables declaration//GEN-END:variables

    private void habilitarCampos(boolean b) {
        btnCategoria.setEnabled(b);
        txtNombre.setEnabled(b);
        txtValorProteico.setEnabled(b);
        ftfStock.setEnabled(b);
        cboUnidad.setEnabled(b);
        lblImagen.setEnabled(b);
        btnGuardar.setEnabled(b);
        btnAgregarSustituto.setEnabled(b);
        btnQuitarSustituto.setEnabled(b);
        cboPrioridad.setEnabled(b);
    }

    private void limpiarCampos() {
        txtBuscar.setText(null);
        txtCategoria.setText(null);
        txtNombre.setText(null);
        txtValorProteico.setText(null);
        ftfStock.setText(null);
        cboUnidad.setSelectedIndex(0);
        cboPrioridad.setSelectedIndex(0);
        lstSustituto.setModel(new DefaultListModel());
        lblImagen.setIcon(new ImageIcon(getClass().getResource("/Imagenes/picture.png")));
    }
}
